//
//  WXIllegalTableViewController.m
//  CarJX
//
//  Created by jxcl on 14-9-17.
//  Copyright (c) 2014年 KK. All rights reserved.
//

#import "UIViewController+HttpRequest.h"
#import "WXIllegalTableViewController.h"
#import "LoginViewController.h"
#import "CLUIBaseNavigationController.h"
 #import "WXAddCarInfoTableViewController.h"
#import "WXVilationTableViewCell.h"
#import "WXTrafficViolationsTableViewController.h"
#import "WXQueryDetailViewController.h"
 #define kVilationIndentifier @"VilalationCar"
@interface WXIllegalTableViewController (){
 }

@end

@implementation WXIllegalTableViewController

+(id)getInstance{
    
    return  [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Illegal"];

}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
       
    }
    return self;
}

- (void)backTo {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void) viewWillAppear:(BOOL)animated{
    [self setTitle:@"我的车辆"];

    self.tableView.tableFooterView = [UIView new];
    [self showNavBarWithRightTitle:@"增加车辆"
                    andRightAction:@selector(presentAddCar)
                     andLeftAction:@selector(backTo)
                       andFontSize:15.0f
                     andRightWidth:80];
    
    [self initTableViewFooter];
    [self.tableView registerNib:[UINib nibWithNibName:@"WXVilationTableViewCell" bundle:nil] forCellReuseIdentifier:kVilationIndentifier];
    
    [self GETWithContentType:@"imicars" params:@{@"memberid" : [Common currentUserId]} success:^(ResultMy *result) {
        _carInfoList = [NSMutableArray arrayWithArray:[NSArray arrayWithArray:result.dictionaryInfo[@"carsinfo"]]] ;
         [self.view hideToastActivity];
        if (_carInfoList.count == 0) {
            return ;
        }else{
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        [self.view hideToastActivity];
        [self.view makeToast:@"获取信息失败" duration:1.3f position:TOP_POINT];
    }];
    [self.view makeToastActivity];
}

- (void) initTableViewFooter{
    NSString *imageName = nil;
    if(iPhone5){
        imageName = @"pic_phone5s";
    }else if (iPhone6 ) {
        imageName = @"pic_phone6";

    }else {
        imageName = @"pic_phone6p(1)";

    }
    
    [self GETWithContentType:@"imipicture" params:@{} success:^(ResultMy *result) {
        
        NSArray *array = result.dictionaryInfo[@"picinfo"];
        
        if ([array count] > 0 ) {
#warning 动态获取图片 第六张
            [_footerImage sd_setImageWithURL:[NSURL URLWithString:[array[[array count] - 1]  customObjectForkey:@"LOGO_URL"]] placeholderImage:[UIImage imageNamed:imageName]];
//           [_footerImage setImageWithURLStr: [array[[array count] - 1]  objectForKey:@"LOGO_URL"]];
            //[_footerImage setImage:[UIImage imageNamed:@"ios_r1_c1_s1.png"]];
        }
        
    } failure:^(NSError *error) {
        [_footerImage setImage:[UIImage imageNamed:imageName]];

    }];
    
}




- (void)viewDidLoad
{
    [super viewDidLoad];
 

}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 2;
}


- (void) presentAddCar{
    
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WXAddCarInfoTableViewController *add = [story instantiateViewControllerWithIdentifier:@"AddCarInfo"];
    [self.navigationController pushViewController:add animated:YES];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    return 61;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *carNumTem = _carInfoList[indexPath.row][@"CAR_NUM"];
    NSString *carNum = [carNumTem substringFromIndex:1];
    NSString *carAear = [carNumTem substringToIndex:1];
    if ([carAear isEqualToString:@"赣"]) {
        WXQueryDetailViewController *detail = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"queryDetail"];
        detail.hpno = carAear;
        detail.hphm = carNum;
        detail.hpzl = [_carInfoList[indexPath.row] objectForKey:@"CAR_TYPE"];
        detail.clsbdh = [_carInfoList[indexPath.row] objectForKey:@"CAR_SN"];
        detail.zhengShuNo = [_carInfoList[indexPath.row] objectForKey:@"CAR_ENG"];
        [self.navigationController pushViewController:detail animated:YES];
    } else{
        WXTrafficViolationsTableViewController *traffic = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"traffic"];
#warning 传从哪里进去的标使;
        traffic.identity = @"登陆";
        traffic.carId = [_carInfoList[indexPath.row] objectForKey:@"ID"] ;
        traffic.carNum = [_carInfoList[indexPath.row] objectForKey:@"CAR_NUM"];
        [self.navigationController pushViewController:traffic animated:YES];
    }
}

#pragma mark - Table view data source

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    return  [_carInfoList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WXVilationTableViewCell *vilation = [tableView dequeueReusableCellWithIdentifier:kVilationIndentifier forIndexPath:indexPath];
    
    vilation.licenseNumber.text = [[_carInfoList objectAtIndex:indexPath.row] objectForKey:@"CAR_NUM"];
    
    return vilation;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleNone;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
    if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
        if (indexPath.row<[self.carInfoList count]) {
            [self deleteOilCard:[[self.carInfoList objectAtIndex:indexPath.row] customObjectForkey:@"ID"]];
            [self.carInfoList removeObjectAtIndex:indexPath.row];//移除数据源的数据
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据
            
        }
    }
}

- (void)deleteOilCard:(NSString *)oilCardNum {
    [self GETWithContentType:@"imidelcar" params:@{@"carid":oilCardNum} success:^(ResultMy *result) {
        [self GETWithContentType:@"imicars" params:@{@"memberid" : [Common currentUserId]} success:^(ResultMy *result) {
            _carInfoList = [NSMutableArray arrayWithArray:[NSArray arrayWithArray:result.dictionaryInfo[@"carsinfo"]]] ;
            [self.view hideToastActivity];
            if (_carInfoList.count == 0) {
                return ;
            }else{
                [self.tableView reloadData];
            }
        } failure:^(NSError *error) {
            [self.view hideToastActivity];
            [self.view makeToast:@"获取信息失败" duration:1.3f position:TOP_POINT];
        }];
        [self.view makeToastActivity];
    } failure:^(NSError *error) {
        
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}




@end
